<?php
// 获取帖子列表
include '../../init.php';

$zid   = @$_GET['zid']; // 板块id
$pid   = @$_GET['pid']; // 帖子id(发帖id)
$page  = @$_GET['page'] - 1;
$count = @$_GET['count'];
if ($count < 1) {
	$count = 10;
} else if ($count > 100) {
	$count = 100;
}

// 查询帖子总数
$sum = array();
$sql = "SELECT count(p.pid) as count FROM `post_tb` as p WHERE p.parentId={$pid} AND p.modid={$zid} ;";
if (!my_mysql_query($sql, $sum)) {
	$count = 0;
}
$sum = $sum[0]['count'] + 1;

if ($page < 1) {
	$page = 0;
} elseif ($page >= floor($sum/$count)){
	$page = floor($sum/$count);
}

// 查询发帖
$sql  = "SELECT p.pid as pid,p.modid as modid,p.uid as uid,p.title as title,p.content as content,p.isTop as top, ";
$sql .= "l.score as score, p.create_time as create_time,u.alias as alias,u.email as email, u.avatarid as avatarid FROM ";
$sql .= "post_tb as p,userinfo_tb as u,login_tb as l WHERE p.pid={$pid} AND p.parentId=0 AND u.uid=p.uid AND l.uid=u.uid LIMIT 1;";
$tops = array();
if (!my_mysql_query($sql, $tops)) {
	echo '发帖不存在';
}

// 查询回帖
$sql = "SELECT p.pid as pid,p.modid as modid,p.uid as uid,p.title as title,p.content as content,p.isTop as top,";
$sql .= "l.score as score, p.create_time as create_time,u.alias as alias,u.email as email, u.avatarid as avatarid FROM ";
$sql .= "post_tb as p,userinfo_tb as u,login_tb as l WHERE (p.parentId={$pid} OR p.pid={$pid}) AND p.modid={$zid} AND u.uid=p.uid AND u.uid=l.uid ORDER BY create_time ASC LIMIT " . ($page * $count) . ",{$count} ;";
$data = array();
$tip = "";
if (!my_mysql_query($sql, $data)) {
	$tip = '没有回帖';
}

// 添加楼层称呼
$call = ['楼主', '沙发', '板凳', '地板', '下水道'];

?>

<!DOCTYPE html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>帖子</title>

	<link rel="stylesheet" type="text/css" href="./css/style_3_common.css">
	<link rel="stylesheet" type="text/css" href="./css/style_3_forum_viewthread.css">
	<link rel="stylesheet" type="text/css" href="./css/style_3_widthauto.css">
	<link rel="stylesheet" type="text/css" href="./css/gfan_bbs.css">
	<link href="./css/bfd_style.css" rel="stylesheet" type="text/css">
	<link href="./css/bdsstyle.css" rel="stylesheet" type="text/css">

	<style type="text/css">
	a:link {
		color: gray;
		text-decoration: none;
	}
	.toolbar {
		height: 48px;
		line-height: 48px;
		color: rgb(130,130,130);
		font-size:13px;
		width: 100%;
	}
    .body-div {
        width:960px;
        margin: 0 auto;
        background-color:#FFFFFF;
    }
    body {
        background-color:#F8F8F8;
    }
	</style>

</head>

<body id="nv_forum" class="pg_viewthread" onkeydown="if(event.keyCode==27) return false;">

<div class="body-div">
	<div id="wp" class="wp">
		<div id="pt" class="bm cl">
			<div class="z">
				<a href="../../index.php">首页</a>
				<span style="color:rgb(210,210,210)">&nbsp;&nbsp;<?php echo $tip; ?></span>
			</div>
		</div>
		<div class="wp cl">
			<div id="pgt" class="pgs mbm cl ">
				<div class="pgt">
					<div class="pg">
					<!-- 分页区 -->
					</div>
					<a id="newspecial" href="../send/index.php?modulID=<?php echo $zid; ?>" title="发新帖">
						<img src="./image/pn_post.png" alt="发新帖">
					</a>
					<a id="post_reply" href="#reply_view" title="回复">
						<img src="./image/pn_reply.png" alt="回复">
					</a>
				</div>
			</div>
		</div>


		<div class="toolbar">
			<form action="index.php" method="GET">
				<input type="hidden" name="zid" value="<?php echo $zid; ?>">
				<input type="hidden" name="pid" value="<?php echo $pid; ?>">共有&nbsp;
				<span style="color:red"><?php echo ceil($sum / $count); ?></span>&nbsp;页,每页有
				<input type="text" size="2" name="count" value="<?php echo $count; ?>">帖子
				当前第&nbsp;<span style="color:red"><?php echo ($page + 1); ?></span>&nbsp;页,跳到第
				<input type="text" size="3" name="page" value="<?php echo $page + 2; ?>">页
				<input type="submit" value="GO">
			</form>
		</div>

		<div id="postlist" class="pl bm">
			<table cellspacing="0" cellpadding="0">
				<tbody>
					<tr>
						<td class="pls ptm pbm"> 标题
						</td>
						<td class="plc ptm pbn vwthd">
							<div class="y">
							</div>
							<h1 class="ts">
								<a href="#" id="thread_subject"><?php echo $tops[0]['title']; ?></a>
							</h1>
							<span class="xg1">
							</span>
						</td>
					</tr>
				</tbody>
			</table>
			<table cellspacing="0" cellpadding="0" class="ad">
				<tbody>
					<tr>
						<td class="pls"></td>
						<td class="plc"></td>
					</tr>
				</tbody>
			</table>

			<!-- 帖子item -->

			<?php
			$i = $page * $count;
			foreach ($data as $value) {
				if (++$i <= count($call)) {
					$rank = $call[$i-1];
				} else {
					$rank = $i.' 楼';
				}
			?>

			<div id="post_302259571">

				<table id="pid302259571" summary="pid302259571" cellspacing="0" cellpadding="0">
					<tbody>

						<!-- 用户 -->
						<tr>
							<td class="pls" rowspan="2">
								<div class="pi">
									<div class="authi"><a href="#" target="_blank" class="xw1"><?php echo $value['alias']; ?></a>
									</div>
								</div>
								<div>
									<div class="avatar">
										<a href="../user/index.php?uid=<?php echo $value['uid']; ?>" target="_blank">
										<img src="../../common/make_avatar.php?uid=<?php echo $value['uid']; ?>"></a>
									</div>
									<div class="tns xg2">
										<table cellspacing="0" cellpadding="0">
											<tbody>
												<tr>
													<td>
														<p><span style="color:red"><?php echo $value['score']; ?></span>&nbsp;积分</p>
													</td>
												</tr>
											</tbody>
										</table>
									</div>
								</div>
								</div>
							</td>
							<td class="plc">
								<div class="pi">
									<strong>
										<a href="#" id="postnum302259571"><?php echo $rank; ?></a>
									</strong>
									<div class="pti">
										<div class="pdbt">
										</div>
										<div class="authi">
											<img class="authicn vm" id="authicon302259571" src="./image/online_member.gif">
											<em id="authorposton302259571">发表于<?php echo date('Y-m-d H:i:s', $value['create_time']); ?></em>
											<span class="xg1"></span>
											<span class="pipe">|</span><a href="#" rel="nofollow">只看该作者</a>
										</div>
									</div>
								</div>
								<div class="pct">
									<div class="pcb">
										<div class="t_fsz">
											<table cellspacing="0" cellpadding="0">
												<tbody>
													<tr>
														<td class="t_f" id="postmessage_302259571">
															<div class="a_pr" style="margin-left:10px;width:auto"></div>
															<?php
															echo $value['content'];
															?>
														</td>
													</tr>
												</tbody>
											</table>
										</div>
										<div id="comment_302259571" class="cm"></div>
										<div id="post_rate_div_302259571"></div>
									</div>
								</div>
							</td>
						</tr>

						<!-- 内容 -->
						<tr>
							<td class="plc plm">
								签名
							</td>
						</tr>

					</tbody>
				</table>
			</div>

			<?php
			}
			?>

		</div>

		<div class="toolbar">
			<form action="index.php" method="GET">
				<input type="hidden" name="zid" value="<?php echo $zid; ?>">
				<input type="hidden" name="pid" value="<?php echo $pid; ?>">共有&nbsp;
				<span style="color:red"><?php echo ceil($sum / $count); ?></span>&nbsp;页,每页有
				<input type="text" size="2" name="count" value="<?php echo $count; ?>">帖子
				当前第&nbsp;<span style="color:red"><?php echo ($page + 1); ?></span>&nbsp;页,跳到第
				<input type="text" size="3" name="page" value="<?php echo $page + 2; ?>">页
				<input type="submit" value="GO">
			</form>
		</div>

		<!-- 使用快捷回复 -->
		<style type="text/css">
		#reply_view{
			height: 270px;
			min-width: 960px;
			margin-right: 0px;
			padding-left: 7px;
			line-height: 32px;
			box-sizing: border-box;
			border: 1px solid rgb(229,229,229);
		}
		#textarea{
			width: 60%;
			height: 80%;
			min-height:150px;
		    outline: none;
			resize: none;
		}
		</style>
		<script charset="utf-8" src="../../plugins/kindeditor/kindeditor-min.js"></script>
		<script charset="utf-8" src="../../plugins/kindeditor/lang/zh_CN.js"></script>
		<script>
			var editor;
			KindEditor.ready(function(K) {
				editor = K.create('#editor-id', {
					resizeType : 0,
					allowPreviewEmoticons : false,
					allowImageUpload : false,
					items : [
						'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
						'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
						'insertunorderedlist', '|', 'emoticons', 'image', 'link']
				});
			});
		</script>
		<div id="reply_view">
			<p>快捷回复</p>
			<form action="../mod_list/post.php" method="POST">
				<input type="hidden" name="parentId" value="<?php echo $tops[0]['pid']; ?>">
				<input type="hidden" name="action" value="revert">
				<textarea id="editor-id" name="content" style="width:700px;height:200px"></textarea>
				<?php
				if (@$_SESSION['isStop']) {
					echo '<span>你被禁言了</span>';
				} else if (@$_SESSION['is_login']) {
					echo '<button type="submit" name="modulID" value="' . $zid . '">回帖</button>';
				} else {
					echo '<span style="font-size:13px;color:rgb(130,130,130)">' .
						'<a href="../login/index.php">登录才能回帖</a></span>';
				}
				?>
			</form>
		</div>

</div>

        </body>
</html>
